angular.module('meBindingServices', [])

  .factory('Binding', function ($resource, $rootScope, ENV, Storage) {

    //设置访问接口路径
    var APIUrl = ENV.api;

    //访问获取用户信息的接口
    var getBindListResource = $resource(APIUrl + "meBindAccount/getBindList.do");
    var updateQqBindStatusResource = $resource(APIUrl + "meBindAccount/updateQqBindStatus.do");
    var updateWeiBoBindStatusResource = $resource(APIUrl + "meBindAccount/updateWeiBoBindStatus.do");
    var updateWeiXinBindStatusResource = $resource(APIUrl + "meBindAccount/updateWeiXinBindStatus.do");

    var bindListJson;
    var bindQqJson;
    var unbindQqJson;
    var bindWeiBoJson;
    var unbindWeiBoJson;
    var bindWeiXinJson;
    var unbindWeiXinJson;

    return {

      getBindList : function(memberId){
        getBindListResource.save(
          {
            member_id : memberId
          },
          function (data) {
            bindListJson = data;
            $rootScope.$broadcast('Binding.getBindList');
          }
        );
      },

      getBindListJson : function(){
        return bindListJson;
      },

      //绑定QQ账号
      bindQq : function (memberId,userid,access_token,qqStatus) {
        updateQqBindStatusResource.save(
          {
            member_id : memberId,
            ab_qq_userid : userid,
            ab_qq_token : access_token,
            ab_qq : qqStatus
          },
          function (data) {
            bindQqJson = data;
            $rootScope.$broadcast('Binding.bindQq');
          }
        );
      },
      getBindQqJson : function (){
        return bindQqJson;
      },

      //解绑QQ账号
      unbindQq : function (memberId,userid,access_token,qqStatus) {
        updateQqBindStatusResource.save(
          {
            member_id : memberId,
            ab_qq_userid : userid,
            ab_qq_token : access_token,
            ab_qq : qqStatus
          },
          function (data) {
            unbindQqJson = data;
            $rootScope.$broadcast('Binding.unbindQq');
          }
        );
      },
      getUnbindQqJson : function (){
        return unbindQqJson;
      },


      //绑定微博
      bindWeiBo : function (memberId,userid,access_token,weiBoStatus) {
        updateWeiBoBindStatusResource.save(
          {
            member_id : memberId,
            ab_weibo_userid : userid,
            ab_weibo_token : access_token,
            ab_weibo : weiBoStatus
          },
          function (data) {
            bindWeiBoJson = data;
            $rootScope.$broadcast('Binding.bindWeiBo');
          }
        );
      },
      getBindWeiBoJson : function(){
        return bindWeiBoJson;
      },


      //解绑微博
      unbindWeiBo : function (memberId,userid,access_token,weiBoStatus) {
        updateWeiBoBindStatusResource.save(
          {
            member_id : memberId,
            ab_weibo_userid : userid,
            ab_weibo_token : access_token,
            ab_weibo : weiBoStatus
          },
          function (data) {
            unbindWeiBoJson = data;
            $rootScope.$broadcast('Binding.unbindWeiBo');
          }
        );
      },
      getUnbindWeiBoJson : function (){
        return unbindWeiBoJson;
      },



      //绑定微信
      bindWeiXin : function (memberId,openid,weiXinStatus) {
        updateWeiXinBindStatusResource.save(
          {
            member_id : memberId,
            ab_weixin_userid : openid,
            ab_weixin_token : "",
            ab_weixin : weiXinStatus
          },
          function (data) {
            bindWeiXinJson = data;
            $rootScope.$broadcast('Binding.bindWeiXin');
          }
        );
      },
      getBindWeiXinJson : function(){
        return bindWeiXinJson;
      },


      //解绑微信
      unbindWeiXin : function (memberId,userid,weiXinStatus) {
        updateWeiXinBindStatusResource.save(
          {
            member_id : memberId,
            ab_weixin_userid : userid,
            ab_weixin_token : "",
            ab_weixin : weiXinStatus
          },
          function (data) {
            unbindWeiXinJson = data;
            $rootScope.$broadcast('Binding.unbindWeiXin');
          }
        );
      },
      getUnbindWeiXinJson : function (){
        return unbindWeiXinJson;
      }


    }
  });
